package com.xingshentech.ugv.operate.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;

import com.xingshentech.ugv.operate.R;
import com.xstech.library.app.AppManager;
import com.xstech.library.utils.PixelUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 自定义标题栏
 * Created by YangZhenshan on 2016/5/5.
 * Edit by YangZhenshan on 2017-8-13 for laceSecret
 */
public class TopBar extends LinearLayout {
    private View rootView;
    private TextView tvTitle;
    private TextView tvRightTxt;
    private ImageButton ibBack;
    private LinearLayout llyRight;
    private TextView v_connect;
    private boolean isConnectVisible;
    private boolean isLandscape;
    private int titleStyle;
    private float titleSize;

    public TopBar(Context context) {
        super(context);
        init(context, null);
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    private void init(Context context, AttributeSet attrs) {
        rootView = View.inflate(context, R.layout.top_view, this);
        initLeftBtn();
        if (!isInEditMode()) {
            rootView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, PixelUtil.Dp2Px(getContext(), 60)));
        }


        if (v_connect == null) {
            v_connect = (TextView) rootView.findViewById(R.id.v_connect);
        }

        if (attrs != null) {
            TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.TopBar);
            String title = ta.getString(R.styleable.TopBar_topBarTitle);
            boolean leftButtonVisible = ta.getBoolean(R.styleable.TopBar_leftButtonVisible, false);
            boolean rightTextVisible = ta.getBoolean(R.styleable.TopBar_rightTextVisible, false);
            isConnectVisible = ta.getBoolean(R.styleable.TopBar_isConnectVisible, false);
            titleStyle = ta.getInteger(R.styleable.TopBar_titleTextStyles, 1);
            titleSize = ta.getDimension(R.styleable.TopBar_titleTextSize, PixelUtil.sp2px(context, 16));
            RelativeLayout relative_layout = findViewById(R.id.relative_layout);
            isLandscape = ta.getBoolean(R.styleable.TopBar_isLandscape, false);
            tvTitle = (TextView) rootView.findViewById(R.id.tvTopTitle);
            if (isLandscape) {
                relative_layout.setBackground(getResources().getDrawable(R.color.black0));
                tvRightTxt = (TextView) rootView.findViewById(R.id.tvTopRightText);
                ibBack = (ImageButton) rootView.findViewById(R.id.ibTopBack);
                tvTitle.setTextColor(getResources().getColor(R.color.white));
                tvTitle.setTextSize(16);
                tvRightTxt.setTextColor(getResources().getColor(R.color.white));
                ibBack.setImageResource(R.mipmap.icon_back_write);
//                findViewById(R.id.view_divide).setVisibility(GONE);
            }
            tvTitle.setTextSize(PixelUtil.px2sp(context, titleSize));
            tvTitle.setTypeface(Typeface.defaultFromStyle(titleStyle));
            ta.recycle();

            setText(title);
            if (!leftButtonVisible) {
                hideLeftBtn();
            }
            if (!rightTextVisible) {
                hideRightText();
            }
            if (isConnectVisible) {
                v_connect.setVisibility(VISIBLE);
            }
        }
    }

    /**
     * 设置标题背景颜色
     *
     * @param resid
     */
    @Override
    public void setBackgroundResource(@DrawableRes int resid) {
        rootView.setBackgroundResource(resid);
    }

    /**
     * 设置标题文字
     *
     * @param resId 文字资源ID
     */
    public void setText(int resId) {
        setText(getResources().getString(resId), null);
    }

    public void setText(String text) {
        setText(text, null);
    }

    /**
     * 设置标题文字
     *
     * @param text 标题文字
     */
    public void setText(String text, OnClickListener onClickListener) {
        if (tvTitle == null) {
            tvTitle = (TextView) rootView.findViewById(R.id.tvTopTitle);
        }
        if (tvTitle != null) {
            tvTitle.setText(text);
        }
        if (onClickListener != null) {
            tvTitle.setOnClickListener(onClickListener);
        }
    }


    /**
     * 获取标题文字内容
     *
     * @return 标题文字
     */
    public String getTitle() {
        if (tvTitle != null) {
            return tvTitle.getText().toString();
        }
        return "";
    }

    /**
     * 获取标题文字内容
     *
     * @return 标题文字
     */
    public String getText() {
        if (v_connect != null) {
            return v_connect.getText().toString();
        }
        return null;
    }


    public TextView getTextView() {
        if (v_connect != null) {
            return v_connect;
        }

        return null;
    }


    public void setTextColor(int color) {
        if (tvTitle != null) {
            tvTitle.setTextColor(color);
        }
    }

    public int getTextColor() {
        if (tvTitle != null) {

            return tvTitle.getCurrentTextColor();
        }
        return 0;
    }


    /**
     * 设置连接状态
     */
    public void isConnect(boolean is) {
        if (isConnectVisible) {
            if (is) {
                v_connect.setText("(连接)");
            } else {
                v_connect.setText("(未连接)");
            }
        }

    }

    public void setLeftBtn() {
        setLeftBtn(R.mipmap.xs_return, () -> AppManager.getAppManager().finishActivity());
    }

    public void setLeftBtn(OnTitleClickListener onClickListener) {
        setLeftBtn(R.mipmap.xs_return, onClickListener);
    }


    /**
     * 设置左侧按钮
     * bitmap <=0 时默认为返回按钮样式
     *
     * @param bitmap          自定义图片资源，传<=0 时默认为返回按钮样式
     * @param onClickListener 点击事件
     */
    @SuppressLint("ResourceType")
    public void setLeftBtn(@DrawableRes int bitmap, final OnTitleClickListener onClickListener) {
        initLeftBtn();
        if (ibBack == null) {
            return;
        }
        ibBack.setVisibility(View.VISIBLE);
        if (bitmap > 0) {
            ibBack.setImageResource(bitmap);
            ibBack.setOnClickListener(view -> onClickListener.onClick());
        }
    }


    /**
     * 隐藏左侧按钮
     */
    public void hideLeftBtn() {
        initLeftBtn();
        ibBack.setVisibility(GONE);
    }

    /**
     * 显示左侧按钮
     * modified by chezi008
     */
    public void showLeftBtn() {
        initLeftBtn();
        ibBack.setVisibility(View.VISIBLE);
    }


    /**
     * 设置右边的文字
     *
     * @param str      文字内容
     * @param listener 点击监听
     */
    public void setRightText(String str, final OnTitleClickListener listener) {
        if (tvRightTxt == null) {
            tvRightTxt = (TextView) rootView.findViewById(R.id.tvTopRightText);
        }

        tvRightTxt.setVisibility(View.VISIBLE);
        tvRightTxt.setText(str);
        tvRightTxt.setOnClickListener(v -> {
            if (listener != null) {
                listener.onClick();
            }
        });
    }

    //    /**
//     * 设置右边的文字
//     *
//     * @param str      文字内容
//     * @param listener 点击监听
//     */
//    public void setRightText0(String str, final OnTitleClickListener listener) {
//        if (tvTopRightText0 == null) {
//            tvTopRightText0 = rootView.findViewById(R.id.tvTopRightText0);
//        }
//        tvTopRightText0.setVisibility(View.VISIBLE);
//        tvTopRightText0.setText(str);
//        tvTopRightText0.setOnClickListener(v -> listener.onClick());
//    }
//
    public void setRightText(String str) {
        if (tvRightTxt == null) {
            tvRightTxt = rootView.findViewById(R.id.tvTopRightText);
        }
        tvRightTxt.setVisibility(View.VISIBLE);
        tvRightTxt.setText(str);
    }
    //
    public void changeTextColor0(int color) {
        if (tvRightTxt == null) {
            tvRightTxt = rootView.findViewById(R.id.tvTopRightText);
        }
        tvRightTxt.setTextColor(color);
    }

    /**
     * 隐藏右边的文字
     */
    public void hideRightText() {
        if (tvRightTxt == null) {
            tvRightTxt = (TextView) findViewById(R.id.tvTopRightText);
        }
        tvRightTxt.setVisibility(View.GONE);
    }


    /**
     * 清除右边所有按钮
     */
    public void resetRightBtns() {
        initRightLayout();
        llyRight.removeAllViews();
        hideRightText();
    }

    /**
     * 根据id隐藏或显示按钮
     *
     * @param id
     */
    public void hideRightBtnById(@IdRes int id, int visible) {
        View btn = findViewById(id);
        if (btn == null) {
            return;
        }
        btn.setVisibility(visible);
    }

    /**
     * 添加右边按钮
     *
     * @param bitmap          按钮资源
     * @param onClickListener 点击回调
     */
    public void addRightBtn(int bitmap, OnClickListener onClickListener) {
        initRightLayout();
        ImageView btn = createBtn(bitmap);
        if (onClickListener != null) {
            btn.setOnClickListener(onClickListener);
        }
        llyRight.addView(btn, 0);
    }

    /**
     * 添加右边按钮
     *
     * @param bitmap          按钮资源
     * @param onClickListener 点击回调
     */
    public void addRightBtn(int bitmap, int id, OnClickListener onClickListener) {
        initRightLayout();
        ImageView btn = createBtn(bitmap);
        btn.setId(id);
        if (onClickListener != null) {
            btn.setOnClickListener(onClickListener);
        }
        llyRight.addView(btn, 0);
    }

    /**
     * 增加视图
     *
     * @param view
     * @param onClickListener
     */
    public void addRightView(View view, OnClickListener onClickListener) {
        initRightLayout();
        if (onClickListener != null) {
            view.setOnClickListener(onClickListener);
        }
        llyRight.addView(view, 0);
    }

    /**
     * 更改左边返回按钮，变其他功能
     *
     * @param bitmap
     * @param onClickListener
     */
    public void addLeftBtn(int bitmap, OnClickListener onClickListener) {
        initLeftBtn();
        ibBack.setImageResource(bitmap);
        if (onClickListener != null) {
            ibBack.setOnClickListener(onClickListener);
        }

    }

    /**
     * 清除所有按钮添加右边按钮
     *
     * @param bitmap          按钮资源
     * @param onClickListener 点击回调
     */
    public void clearAndAddRightBtn(int bitmap, OnClickListener onClickListener) {
        initRightLayout();
        resetRightBtns();
        ImageView btn = createBtn(bitmap);
        if (onClickListener != null) {
            btn.setOnClickListener(onClickListener);
        }
        llyRight.addView(btn);
    }

    /**
     * 获取所有view的list
     *
     * @return
     */
    public List<View> getRightBtnList() {
        List<View> views = new ArrayList<>();
        for (int i = 0; i < llyRight.getChildCount(); i++) {
            views.add(llyRight.getChildAt(i));
        }
        return views;
    }

    private void initRightLayout() {
        if (llyRight == null) {
            llyRight = (LinearLayout) findViewById(R.id.lly_top_right);
        }
    }

    private void initLeftBtn() {
        if (ibBack == null) {
            ibBack = (ImageButton) findViewById(R.id.ibTopBack);
            ibBack.setOnClickListener(view -> {
                try {
                    ((Activity) ibBack.getContext()).onBackPressed();
                } catch (Exception e) {
                }
            });
        }
    }

    private ImageView createBtn(int bitmap) {
        if (bitmap <= 0) {
            throw new IllegalArgumentException("invalid resId");
        }
        ImageView button = new ImageView(getContext());
        button.requestFocus();
        button.setImageResource(bitmap);
        button.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        int padding = PixelUtil.Dp2Px(getContext(), 12);
        int paddingLeft = PixelUtil.Dp2Px(getContext(), 12);
        button.setPadding(paddingLeft, padding, padding, padding);
        return button;
    }

    public interface OnTitleClickListener {
        void onClick();
    }

    public LinearLayout getLlyRight() {
        return llyRight;
    }
}


